package com.itheima.exception;

import com.itheima.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * GlobalExceptionHandler
 *
 * @author liliudong
 * @version 1.0
 * @description 全局异常捕获
 * @date 2023/7/18 16:15
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result<?> exception(Exception exception) {
        log.error(exception.getMessage(), exception);
        return Result.error("未知异常!");
    }

    @ExceptionHandler(RuntimeException.class)
    public Result<?> exception(RuntimeException e) {
        log.error(e.getMessage(), e);
        return Result.error(e.getMessage());
    }

    /**
     * 自定义 好处：每个错误都在预期之中
     *
     * @param exception
     * @return
     */
    @ExceptionHandler(BaseException.class)
    public Result<?> baseException(BaseException exception) {
        log.error(exception.getMsg(), exception);
        log.info("=======================");
        return Result.error(exception.getMsg(), exception.getCode());
    }
}
